package cn.lyjuan.first.netty.demo._2_netty;

import cn.lyjuan.first.netty.demo._2_netty.selector.AbstractNIOSelector;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class BossSelector extends AbstractNIOSelector
{
    protected Integer[] selectionKeys()
    {
        return new Integer[]{SelectionKey.OP_ACCEPT};
    }

    /**
     * 新连接请求
     *
     * @param selectionKey
     */
    protected void process(SelectionKey selectionKey)
    {
        if (!selectionKey.isAcceptable())
            return;

        // 注册读取监听器
        ServerSocketChannel chl = (ServerSocketChannel) selectionKey.channel();

        try
        {
            SocketChannel client = chl.accept();
            this.pool.nextWorker().register(client);
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
